package com.d2.modules.system.dict.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.d2.common.domain.Result;
import com.d2.modules.system.dict.entity.Dict;
import com.d2.modules.system.dict.entity.DictItem;
import com.d2.modules.system.dict.service.DictItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("dictItem")
public class DictItemController {

    @Autowired
    private DictItemService dictItemService;

    @GetMapping("list")
    public Result<List<DictItem>> list(DictItem query){
        LambdaQueryWrapper<DictItem> qw = Wrappers.lambdaQuery(query);
        qw.orderByAsc(DictItem::getSort);
        return Result.success(dictItemService.list(qw));
    }

    @GetMapping("page")
    public Result<Page<DictItem>> list(Page<DictItem> page, DictItem query){
        LambdaQueryWrapper<DictItem> qw = Wrappers.lambdaQuery(query);
        qw.orderByAsc(DictItem::getSort);
        return Result.success(dictItemService.page(page,qw));
    }

    @PostMapping("add")
    public Result<Void> add(@RequestBody DictItem obj){
        dictItemService.save(obj);
        return Result.success();
    }

    @PutMapping("update")
    public Result<Void> update(@RequestBody DictItem obj){
        dictItemService.updateById(obj);
        return Result.success();
    }

    @DeleteMapping("delete/{id}")
    public Result<Void> delete(@PathVariable Long id){
        dictItemService.removeById(id);
        return Result.success();
    }
}
